package com.example.practice.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.practice.R;
import com.example.practice.bean.UserBean;
import com.example.practice.utils.DBUtils;
import com.example.practice.utils.SharedpreferencesUtils;

public class UserInfoActivity_yl extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_title;
    private TextView tv_back;
    private RelativeLayout rl_title_bar;
    private TextView tv_userName_yl, tv_nickName_yl, tv_sex_yl,tv_signature_yl;
    private RelativeLayout rl_nickName_yl,rl_sex_yl,rl_signature_yl;
    private String spUserName_yl;
    private String new_info_yl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info_yl);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        spUserName_yl= SharedpreferencesUtils.readLoginUserName(this);
        initView();
        initData();
        setListener();
    }

    /**
     * 设置控件点击监听事件
     */
    private void setListener() {
        tv_back.setOnClickListener(this);
        rl_nickName_yl.setOnClickListener(this);
        rl_sex_yl.setOnClickListener(this);
        rl_signature_yl.setOnClickListener(this);
    }

    /**
     * 更新数据库的性别信息并显示到页面
     */
    private void setSex(String sex){
        tv_sex_yl.setText(sex);
        //更新数据库中的性别字段
        DBUtils.getInstance(UserInfoActivity_yl.this).updateUserInfo("sex",sex,spUserName_yl);

    }

    /**
     * 设置性别的弹出框
     */
    private void sexDialog(String sex){
        int sexFlag=0;
        if("男".equals(sex)){
            sexFlag=0;
        }
        else if ("女".equals(sex)){
            sexFlag=1;
        }
        final String items[]={"男","女"};
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("性别");
        builder.setSingleChoiceItems(items, sexFlag, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Toast.makeText(UserInfoActivity_yl.this,items[which],Toast.LENGTH_SHORT).show();
                setSex(items[which]);
            }
        });
        builder.create().show();
    }

    private void initView() {
        tv_title=findViewById(R.id.tv_title);
        tv_title.setText("个人资料");
        tv_back=findViewById(R.id.tv_back);
        rl_title_bar=findViewById(R.id.rl_bar);
        rl_title_bar.setBackgroundColor(Color.parseColor("#30b4ff"));
        rl_nickName_yl=findViewById(R.id.rl_nickName_yl);
        rl_sex_yl=findViewById(R.id.rl_sex_yl);
        rl_signature_yl=findViewById(R.id.rl_signature_yl);
        tv_userName_yl=findViewById(R.id.tv_userName_yl);
        tv_nickName_yl=findViewById(R.id.tv_nickName_yl);
        tv_sex_yl=findViewById(R.id.tv_sex_yl);
        tv_signature_yl=findViewById(R.id.tv_signature_yl);
    }

    /**
     * 将用户信息显示到界面上
     * @param bean
     */
    private void setValue(UserBean bean){
        tv_userName_yl.setText(bean.userName);
        tv_nickName_yl.setText(bean.nickName);
        tv_sex_yl.setText(bean.sex);
        tv_signature_yl.setText(bean.signature);
    }

    private void initData() {
        UserBean bean =null;
        bean= DBUtils.getInstance(this).getUserInfo(spUserName_yl);
//        首先判断数据库是否有数据
        if(bean==null){
            bean=new UserBean();
            bean.userName=spUserName_yl;
            bean.nickName="学习助手";
            bean.sex="女";
            bean.signature="学习助手";
            //保存用户信息到数据库
            DBUtils.getInstance(this).saveUserInfo(bean);
        }
        setValue(bean);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                this.finish();
                break;
            case R.id.rl_nickName_yl:
                String nickName=tv_nickName_yl.getText().toString();
                Bundle bdName=new Bundle();
                bdName.putString("content", nickName);
                bdName.putString("title", "昵称");
                bdName.putInt("flag",1);
                enterActivityForResult(changeUserInfoActivity_yl.class,1,bdName);
                break;
            case R.id.rl_sex_yl:
                String sex=tv_sex_yl.getText().toString();
                sexDialog(sex);
                break;
            case R.id.rl_signature_yl:
                String signature=tv_signature_yl.getText().toString();
                Bundle bdSignature=new Bundle();
                bdSignature.putString("content", signature);
                bdSignature.putString("title","签名");
                bdSignature.putInt("flag",2);
                enterActivityForResult(changeUserInfoActivity_yl.class,2,bdSignature);
                break;
            default:
                break;
        }
    }

    /**
     * 获取回传数据需使用的的跳转方法
     * 第一个参数to表示需要跳转的界面,第二个参数requestCode表示一个请求码,第三个参数b表示跳转时的传递的数据
     */
    public void enterActivityForResult(Class<?> to, int requestCode, Bundle b){
        Intent i=new Intent(this, to);
        i.putExtras(b);
        startActivityForResult(i,requestCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1:
                if (data!=null){
                    new_info_yl=data.getStringExtra("nickName");
                    if (TextUtils.isEmpty(new_info_yl) || new_info_yl==null){
                        return;
                    }
                    tv_nickName_yl.setText(new_info_yl);
                    DBUtils.getInstance(UserInfoActivity_yl.this).updateUserInfo("nickName",new_info_yl,spUserName_yl);
                }
                break;
            case 2:
                if(data!=null){
                    new_info_yl=data.getStringExtra("signature");
                    if (TextUtils.isEmpty(new_info_yl) || new_info_yl==null){
                        return;
                    }
                    tv_signature_yl.setText(new_info_yl);
                    DBUtils.getInstance(UserInfoActivity_yl.this).updateUserInfo("signature", new_info_yl,spUserName_yl);
                }
                break;
        }
    }
}